package com.wl.shop.web;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wl.shop.entity.Role;
import com.wl.shop.entity.User;
import com.wl.shop.entity.UserRole;
import com.wl.shop.model.DataTableResult;
import com.wl.shop.model.RestResponse;
import com.wl.shop.model.RoleVo;
import com.wl.shop.utils.Constants;
import com.wl.shop.utils.UserUtils;

@Controller
@RequestMapping("${adminPath}/role")
public class RoleController extends BaseController {
	
	@RequestMapping(value = "list",method=RequestMethod.GET)
	public String list(Model model){
		logger.info("跳转角色列表---");
		return "sys/role/list";
	}
	
	@RequestMapping(value = "list",method=RequestMethod.POST)
	@ResponseBody
	public DataTableResult list(String aodata){
		logger.info("设置datatable的值",aodata);
		Map<String,Object>	map =Constants.convertToMap(aodata);
		int echo = (Integer) map.get("sEcho");
		int start = (Integer)map.get("iDisplayStart");
		int pageSize = (Integer)map.get("iDisplayLength");
		int pageNum = start/pageSize<=0?1:(start/pageSize+1);
		pageSize = pageSize>0?pageSize:10;
		DataTableResult result = new DataTableResult();
		result.setSEcho(echo);
		EntityWrapper<Role> wapper = new EntityWrapper<Role>();
		wapper.eq("is_del", false);
		Page<Role> page  =roleService.listByParam(new Page<Role>(pageNum,pageSize), wapper);
		result.setAaData(page.getRecords());
		result.setITotalRecords(page.getTotal());
		result.setITotalDisplayRecords(page.getTotal());
		return result;
	}
	
	@RequestMapping(value = "add",method=RequestMethod.GET)
	public String add(Model model){
		logger.info("跳转新增角色---");
		model.addAttribute("treeData", UserUtils.getMenuTreeString());
		return "sys/role/add";
	}
	
	@RequestMapping(value = "add",method = RequestMethod.POST)
	@ResponseBody
	public RestResponse save(@RequestBody RoleVo role){
		if(StringUtils.isBlank(role.getName())){
			return RestResponse.failure("角色名称不能为空");
		}
		List<String> menuIds = role.getMenuIds();
		if(menuIds == null || menuIds.size()==0){
			return RestResponse.failure("权限选择不能为空");
		}
		EntityWrapper<Role> wrapper = new EntityWrapper<Role>();
		wrapper.eq("is_del", false).eq("name", role.getName());
		List<Role> list = roleService.listByParam(wrapper);
		if(list != null && list.size()>0){
			return RestResponse.failure("角色名称已存在");
		}
		Role r = new Role();
		r.setName(role.getName());
		r.setEnname(role.getEnname());
		roleService.saveOrUpdate(r);
		if(StringUtils.isNotBlank(r.getId())){
			roleMenuService.saveRoleMenu(r.getId(), StringUtils.join(menuIds, ","));
		}
		return RestResponse.success();
	}
	
	@RequestMapping(value = "edit",method = RequestMethod.GET)
	public String edit(String id,Model model){
		logger.info("跳转编辑角色---",id);
		Role  role = roleService.selectById(id);
		if(role !=null){
			model.addAttribute("role", role);
		}
		List<String> menuIds = roleMenuService.getMenuIdsByRoleId(id);
		if(menuIds != null && menuIds.size()>0){
			model.addAttribute("menuIds", StringUtils.join(menuIds, ","));
		}
		model.addAttribute("treeData", UserUtils.getMenuTreeString());
		return "sys/role/edit";
	}
	
	@RequestMapping(value = "edit",method = RequestMethod.POST)
	@ResponseBody
	public RestResponse edit(@RequestBody RoleVo role){
		if(StringUtils.isBlank(role.getId())){
			return RestResponse.failure("角色ID为空");
		}
		if(StringUtils.isBlank(role.getName())){
			return RestResponse.failure("角色名称不能为空");
		}
		List<String> menuIds = role.getMenuIds();
		if(menuIds == null || menuIds.size()==0){
			return RestResponse.failure("权限选择不能为空");
		}
		Role old = roleService.selectById(role.getId());
		if(old == null){
			return RestResponse.failure("数据出错");
		}
		if(!old.getName().equals(role.getName())){
			EntityWrapper<Role> wrapper = new EntityWrapper<Role>();
			wrapper.eq("is_del", false).eq("name", role.getName());
			List<Role> list = roleService.listByParam(wrapper);
			if(list != null && list.size()>0){
				return RestResponse.failure("角色名称已存在");
			}
		}
		Role r = new Role();
		r.setId(role.getId());
		r.setName(role.getName());
		r.setEnname(role.getEnname());
		roleService.saveOrUpdate(r);
		if(StringUtils.isNotBlank(r.getId())){
			roleMenuService.saveRoleMenu(r.getId(), StringUtils.join(menuIds, ","));
		}
		return RestResponse.success();
	}
	
	@RequestMapping(value = "delete",method=RequestMethod.POST)
	@ResponseBody
	public RestResponse delete(@RequestParam(value="id[]",required=false)List<String> id){
		if(id == null || id.size()==0){
			return RestResponse.failure("id不能为空");
		}
		for (String roleId : id) {
			if("1".equals(roleId)){
				return RestResponse.failure("超级管理员角色不能删除");
			}
			Role role = roleService.selectById(roleId);
			if(role == null){
				return RestResponse.failure("数据出错");
			}
			role.deleteById();
			Map<String, Object> map = Maps.newHashMap();
			map.put("role_id", roleId);
			roleMenuService.deleteByMap(map);
		}
		return RestResponse.success();
	}
}
